/**
 * 去掉对象里面所有字符串的前后空格
 * @returns {object}
 */
function trimObjStr(param) {
    var obj = {};
    for (var key in param) {
        var value = param[key];
        if (typeof value == 'string') {
            obj[key] = value.trim(value);
        } else {
            obj[key] = value;
        }
    }
    return obj;
}

function isNumber(obj) {
    if (typeof obj == 'number') {
        return true;
    }
    return false;
}

function isJsonObj(obj) {
    if (
        typeof obj == 'object' &&
        Object.prototype.toString.call(obj).toLowerCase() == '[object object]' &&
        !obj.length
    ) {
        return true;
    }
    return false;
}

function isArray(obj) {
    return obj.constructor == Array
}

function isEmpty(obj) {
    if (obj === '' || obj === null || obj === undefined) {
        return true;
    }
    return false;
}

function num2str(jsonobj) {
    let jsonobj_tmp = jsonobj;
    for (var key in jsonobj_tmp) {
        if (isJsonObj(jsonobj_tmp[key])) {
            jsonobj_tmp[key] = num2str(jsonobj_tmp[key]);
        } else if (isNumber(jsonobj_tmp[key])) {
            jsonobj_tmp[key] = jsonobj_tmp[key] + '';
        }
    }
    return jsonobj_tmp;
}

function arrayValue2str(obj) {
    let array = [];
    obj.forEach(e => {
        if (isArray(e)) {
            let newe = arrayValue2str(e);
        } else if (isJsonObj(e)) {
            let newe = num2str(e);
            array.push(newe);
        } else if (isNumber(e)) {
            let newe = e.toString();
            array.push(newe);
        }
    });
    return array;
}

function toggleClass(dom, classname) {
    if (dom.classList.contains(classname)) {
        dom.classList.remove(classname);
    } else {
        dom.classList.add(classname);
    }
}

function getLoginer() {
    return JSON.parse(sessionStorage.getItem('loginInfo'));
}

function clearLoginer() {
    sessionStorage.removeItem('loginInfo');
}

function listhas(list,value,key){
    return new Promise((resolve, reject)=>{
        list.forEach((item,i)=>{
            if(item[key]==value){
                resolve(true);
            }
            if(i == list.length){
                reject(false);
            }
        })
    })
}
function getQuery(name) {
    // 正则：[找寻'&' + 'url参数名字' = '值' + '&']（'&'可以不存在）
    let reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    let r = window.location.search.substr(1).match(reg);//substr() 的参数指定的是子串的开始位置和长度，因此它可以替代 substring() 和 slice() 来使用,但是它没有标准化
    if(r != null) {
    // 对参数值进行解码
        return unescape(r[2]); 
    }
    return null;
}

function loadingshow() {
    let element = document.getElementById('loadingPlugin');
    if (element != null && element != undefined) {
        element.style.display="flex";
    }
}

function loadinghide() {
    let element = document.getElementById('loadingPlugin');
    if (element != null && element != undefined) {
        element.style.display="none";
    }
}

export default {
    trimObjStr: trimObjStr,
    isNumber: isNumber,
    isJsonObj: isJsonObj,
    isArray: isArray,
    isEmpty: isEmpty,
    num2str: num2str,
    toggleClass: toggleClass,
    arrayValue2str: arrayValue2str,
    getLoginer: getLoginer,
    clearLoginer: clearLoginer,
    getQuery:getQuery,
    loadingshow: loadingshow,
    loadinghide: loadinghide
};